import java.util.Vector;

import TUIO.TuioCursor;
import TUIO.TuioPoint;
import TUIO.TuioProcessing;
import ddf.minim.*;
import processing.core.*;
/*
public class Waveform
{*/
 // This class is a very simple implementation of AudioListener. By implementing this interface, 
 // you can add instances of this class to any class in Minim that implements Recordable and receive
 // buffers of samples in a callback fashion. In other words, every time that a Recordable object has 
 // a new buffer of samples, it will send a copy to all of its AudioListeners. You can add an instance of 
 // an AudioListener to a Recordable by using the addListener method of the Recordable. If you want to 
 // remove a listener that you previously added, you call the removeListener method of Recordable, passing 
 // the listener you want to remove.
 //
 // Although possible, it is not advised that you add the same listener to more than one Recordable. 
 // Your listener will be called any time any of the Recordables you've added it have new samples. This 
 // means that the stream of samples the listener sees will likely be interleaved buffers of samples from 
 // all of the Recordables it is listening to, which is probably not what you want.
 //
 // You'll notice that the three methods of this class are synchronized. This is because the samples methods 
 // will be called from a different thread than the one instances of this class will be created in. That thread 
 // might try to send samples to an instance of this class while the instance is in the middle of drawing the 
 // waveform, which would result in a waveform made up of samples from two different buffers. Synchronizing 
 // all the methods means that while the main thread of execution is inside draw, the thread that calls 
 // samples will block until draw is complete. Likewise, a call to draw will block if the sample thread is inside 
 // one of the samples methods. Hope that's not too confusing!

 class WaveformRenderer implements AudioListener
 {
     TuioProcessing tuioClient;
   private float[] left;
   private float[] right;
   PApplet parent; // The parent PApplet that we will render ourselves onto
   WaveformRenderer(PApplet p)
   {
       parent=p;
     left = null; 
     right = null;
 //    tuioClient  = new TuioProcessing(this);
   }
   
   public synchronized void samples(float[] samp)
   {
     left = samp;
   }
   
   public synchronized void samples(float[] sampL, float[] sampR)
   {
     left = sampL;
     right = sampR;
   }
   
   synchronized void draw()
   {
       
     // we've got a stereo signal if right or left are not null
     if ( left != null && right != null )
     {
       parent.noFill();
       parent.stroke(255);
       parent.beginShape();
       for ( int i = 0; i < left.length; i++ )
       {
         parent.line(i, parent.height, i, /* parent.height/2 + */ parent.height-left[i]*255);
         int pink = parent.color(255,200,100);
         parent.stroke(pink);
         parent.vertex(i, parent.height, i, /* parent.height/2 + */ parent.height-left[i]*255);
       //  parent.stroke(255);
       }
       parent.endShape();
     //  parent.beginShape();
   /*    for ( int i = 0; i < right.length; i++ )
       {
         parent.vertex(i, 3*(parent.height/4) + right[i]*50);
       }*/
   //    parent.endShape();
     }
   /*  else if ( left != null )
     {
         parent.noFill();
         parent.stroke(255);
         parent.beginShape();
       for ( int i = 0; i < left.length; i++ )
       {
         parent.vertex(i, parent.height/2 + left[i]*50);
       }
       parent.endShape();
     }*/
     Vector tuioCursorList = tuioClient.getTuioCursors();
     for (int i=0;i<tuioCursorList.size();i++) {
           TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
           Vector pointList = tcur.getPath();

           if (pointList.size()>0) {
                 parent.stroke(0,0,255);
                 TuioPoint start_point = (TuioPoint)pointList.firstElement();;
                 for (int j=0;j<pointList.size();j++) {
                       TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
                    //   line(start_point.getScreenX(parent.width),start_point.getScreenY(parent.height),end_point.getScreenX(parent.width),end_point.getScreenY(parent.height));
                       start_point = end_point;
                 }

                 parent.stroke(192,192,192);
                 parent.fill(192,192,192);
                 float cursor_size = 15;
                 float scale_factor = 1;
                 float cur_size = cursor_size*scale_factor; 
                 parent.ellipse( tcur.getScreenX( parent.width), tcur.getScreenY( parent.height),cur_size,cur_size);
                 parent.fill(0);
                 parent.text(""+ tcur.getCursorID(),  tcur.getScreenX( parent.width)-5,  tcur.getScreenY( parent.height)+5);
           }
     }
   }
 }

/*}*/
